package info.matsumana.sandbox;

import com.mysql.fabric.jdbc.FabricMySQLConnection;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * Fabricサーバに接続してデータを生成するツール
 *
 * @author matsumana
 */
public class Insert extends DatabaseAccess {

    public static void main(String... args) {

        String a_sql = "INSERT INTO a(column_2,column_3,column_4) VALUES (?, ?,?)";
        String b_sql = "INSERT INTO b(column_2,column_7,column_8) VALUES (?, ?,?)";
        String c_sql = "INSERT INTO c(column_12,column_13,column_14) VALUES (?, ?,?)";
        FabricMySQLConnection conn = null;
        Long start = System.currentTimeMillis();
        try {
            conn = getConnection();
            PreparedStatement ps = conn.prepareStatement(b_sql);
            for (int i = 1; i < 100; i++) {
                conn.setShardKey(String.valueOf(i));
                ps.setInt(1, i);
                ps.setDate(2, new Date(System.currentTimeMillis()));
                ps.setString(3, "这是第" + i + "ge");
                System.out.println("这是第" + i + "ge");
                ps.executeUpdate();
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            Long end = System.currentTimeMillis();
            System.out.println("开始时间:" + start + ",结束时间:" + end + ".总用时:" + (end - start));
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
